package org.jeecg.modules.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.ding.DingApi;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.entity.*;
import org.jeecg.modules.mapper.DiyProfessorInfoMapper;
import org.jeecg.modules.mapper.DiyProjectProfessorMapper;
import org.jeecg.modules.mapper.ProjectProfessorInfoMapper;
import org.jeecg.modules.service.IProjectProfessorInfoService;
import org.jeecg.modules.vo.ProjectProfessorStar;
import org.jeecg.modules.vo.SelectForEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description: 项目所需专家抽取状态
 * @Author: jeecg-boot
 * @Date:   2020-07-06
 * @Version: V1.0
 */
@Service
@Slf4j
public class ProjectProfessorInfoServiceImpl extends ServiceImpl<ProjectProfessorInfoMapper, ProjectProfessorInfo> implements IProjectProfessorInfoService {

    @Autowired
    DiyProfessorInfoMapper diyProfessorInfoMapper;
    @Autowired
    ProjectProfessorInfoMapper projectProfessorInfoMapper;
    @Autowired
    DiyProjectProfessorMapper diyProjectProfessorMapper;
    @Autowired
    DiyProjectOwnProfessorTypeServiceImpl diyProjectOwnProfessorTypeService;
    @Autowired
    private DingApi dingApi;


 /*   @Override
    public IPage<ProjectProfessorInfo> page(IPage<ProjectProfessorInfo> page, Wrapper<ProjectProfessorInfo> queryWrapper) {

        IPage<ProjectProfessorInfo> projectProfessorInfoIPage = projectProfessorInfoMapper.selectPage(page, queryWrapper);
        log.info("projectProfessorInfoIPage ==> {}",projectProfessorInfoIPage);
        // 修正专家类型中文，添加专家姓名
        List<ProjectProfessorInfo> records = projectProfessorInfoIPage.getRecords();
        for(ProjectProfessorInfo projectProfessorInfo :records){
            // 获取项目评分需要的专家信息
            List<ProjectProfessorStar> projectOwnProfessorList = projectProfessorInfo.getProjectOwnProfessorList();
            // 遍历每条专家信息
            for (ProjectProfessorStar professor:projectOwnProfessorList) {
                // 修正类型
                professor.setType(diyProfessorInfoMapper.selDictNameById(professor.getType()));
                // 获取姓名
                QueryWrapper<DiyProfessorInfo> wrapper = new QueryWrapper<>();
                wrapper.eq("ding_user_id",professor.getKey());
                DiyProfessorInfo info = diyProfessorInfoMapper.selectOne(wrapper);
                professor.setName(info.getProfessorName());
            }

        }

        return projectProfessorInfoIPage;
    }*/

    @Override
    public List<ProjectProfessorInfo> selectByProjectName(String projectName) {
        return projectProfessorInfoMapper.selectByProjectName(projectName);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateComment(String projectId, String comment, List<ProjectProfessorStar> professors,String files) {
        // 保存对项目的评论
        ProjectProfessorInfo info = new ProjectProfessorInfo();
        info.setId(projectId);
        info.setProjectComment(comment);
        info.setProjectStatus(ProjectProfessorInfo.projectStage.OVER);
        // 更新项目结束的附件路径
        info.setProjectOverFiles(files);

        try {
            projectProfessorInfoMapper.updateById(info);
            // 保存对参加项目的专家们的评分
            professors.forEach(professor->{
                //获取当前的时间
                SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date(System.currentTimeMillis());
                System.out.println(date);
                //将当前的时间和评价存入 专家项目评分表里
                UpdateWrapper<DiyProjectProfessor> wrapper = new UpdateWrapper<>();
                wrapper.set("professor_in_project_professor_star",professor.getValue())
                        .set("create_time",date)
                        .eq("project_id",projectId)
                        .eq("ding_user_id",professor.getKey());
                diyProjectProfessorMapper.update(null,wrapper);

            });
            // 成功返回1
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            // 失败返回0
            return 0;
        }
    }

    @Override
    public IPage<ProjectProfessorInfo> pageByEntity(Page<ProjectProfessorInfo> page, SelectForEntity selectForEntity) {
        List<ProjectProfessorInfo> projectProfessorInfoIPage = projectProfessorInfoMapper.pageByEntity(selectForEntity);
        // 修正专家类型中文，添加专家姓名
        if (projectProfessorInfoIPage.size() > 0) {
            for(ProjectProfessorInfo projectProfessorInfo :projectProfessorInfoIPage){
                // 获取项目评分需要的专家信息
                List<ProjectProfessorStar> projectOwnProfessorList = projectProfessorInfo.getProjectOwnProfessorList();
                // 遍历每条专家信息
                for (ProjectProfessorStar professor:projectOwnProfessorList) {
                    // 修正类型
                    professor.setType(diyProfessorInfoMapper.selDictNameById(professor.getType()));
                    // 获取姓名
                    String dingUserId = professor.getKey();
                    DiyProfessorInfo info = diyProfessorInfoMapper.selectInfoByDIngUserId(dingUserId);
                    professor.setName(info.getProfessorName());
                }

            }

        }
//        //手动调整分页
//        //获取当前的分页页数
//        long current = page.getCurrent();
//        //获取当前一页显示的总数
//        long size = page.getSize();
//        //获取当前库中总记录数
//        long dbTableSize = projectProfessorInfoIPage.size();
//        //获取当前分页需要的起始数据下标
//        long beginIndex=(page.getCurrent()-1)*10;
//        List<ProjectProfessorInfo> record = new ArrayList<ProjectProfessorInfo>();
//        for (long i =beginIndex ; i <dbTableSize  ; i++) {
//            record.add(projectProfessorInfoIPage.get((int)i));
//        }
//        page.setRecords(record);
        page.setRecords(projectProfessorInfoIPage);
        return page;
    }
    @Override
    public  void thinkNumberReduce(String projectId,String professorType,int num){
        //获取该专家在项目中的专家项目类型数据
        String thinkProfeesor = diyProjectOwnProfessorTypeService.getOne(new QueryWrapper<DiyProjectOwnProfessorType>().lambda().
                eq(DiyProjectOwnProfessorType::getProjectId, projectId).
                eq(DiyProjectOwnProfessorType::getProfessorType, professorType)).getThinkProfessorNumber();
        int thinkNum = Integer.parseInt(thinkProfeesor);
        //更改项目中该专家对应的项目类型的考虑中人数
        diyProjectOwnProfessorTypeService.update(null, new UpdateWrapper<DiyProjectOwnProfessorType>().lambda().
                eq(DiyProjectOwnProfessorType::getProjectId, projectId).
                eq(DiyProjectOwnProfessorType::getProfessorType, professorType).
                set(DiyProjectOwnProfessorType::getThinkProfessorNumber, Integer.toString(thinkNum+num)));
    }

    @Override
    public IPage<ProjectProfessorInfo> pageByProfessorUserId(Page<ProjectProfessorInfo> page, LoginUser user) {
        // 获取userId
        String useridStr = dingApi.selUserIdByUnionid(user.getUsername());
        JSONObject getUserId = JSON.parseObject(useridStr);
        String userid = getUserId.getString("userid");

        return projectProfessorInfoMapper.queryListByUserId(page,userid);
    }
    @Override
    public IPage<ProjectProfessorInfo> pageByProfessorUserId01(Page<ProjectProfessorInfo> page, LoginUser user) {
        // 获取userId
        String useridStr = dingApi.selUserIdByUnionid(user.getUsername());
        JSONObject getUserId = JSON.parseObject(useridStr);
        String userid = getUserId.getString("userid");

        return projectProfessorInfoMapper.queryListByUserId01(page,userid);
    }
    @Override
    public IPage<ProjectProfessorInfo> pageByProfessorUserId03(Page<ProjectProfessorInfo> page, LoginUser user,String projectName) {
        // 获取userId
        String useridStr = dingApi.selUserIdByUnionid(user.getUsername());
        JSONObject getUserId = JSON.parseObject(useridStr);
        String userid = getUserId.getString("userid");

        return projectProfessorInfoMapper.queryListByUserId03(page,userid,projectName);
    }
}
